<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Helper Classes in wxWidgets</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="wxWidgets, tutorial, helper classes, C++, programming, GUI, multiplatform">
<meta name="description" content="This part of the wxWidgets tutorial covers helper classes.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>wxWidgets helper classes</h1>

<p>
wxWidgets consists of a large group of helper classes, that help 
programmers to do their job. These include classes for working 
with strings, files, XML files, streams, database or network.
Here we will show only a tiny drop of the whole lake.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
wxWidgets library can be used to create console and gui applications. In 
this chapter, we will illustrate some of the 
helper classes in console based applications.
</p>


<h2>Console</h2>

<p>
This is a simple console application. The application puts some 
text into the console window.
</p>

<div class="codehead">console.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{
  wxPuts(wxT("A wxWidgets console application"));
}
</pre>

<pre>
A wxWidgets console application
</pre>

<p>
Output.
</p>


<h2>wxString</h2>

<p>
wxString is a class representing a character string.
</p>

<p>
In the following example, we define three wxStrings. We create one string 
of these strings using addition operation.
</p>

<div class="codehead">addition.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{
  wxString str1 = wxT("Linux");
  wxString str2 = wxT("Operating");
  wxString str3 = wxT("System");

  wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;

  wxPuts(str);
}
</pre>


<pre>
Linux Operating System
</pre>

<p>
Output.
</p>

<p>
The Printf() method is used to format strings. 
</p>

<div class="codehead">formatted.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{

  int flowers = 21;

  wxString str;
  str.Printf(wxT("There are %d red roses."), flowers);
  
  wxPuts(str);
}
</pre>

<pre>
There are 21 red roses.
</pre>

<p>
Output.
</p>

<p>
The following example checks, whether a string contains another string. 
For this we have a <i>Contains()</i> method.
</p>

<div class="codehead">contains.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{

  wxString str = wxT("The history of my life");

  if (str.Contains(wxT("history"))) {
      wxPuts(wxT("Contains!"));
  }


  if (!str.Contains(wxT("plain"))) {
      wxPuts(wxT("Does not contain!"));
  }

}
</pre>

<pre>
Contains!
Does not contain!
</pre>

<p>
Output.
</p>

<p>
The <i>Len()</i> method returns the number of characters in the string.
</p>

<div class="codehead">length.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{
  wxString str = wxT("The history of my life");
  wxPrintf(wxT("The string has %d characters\n"), str.Len());
}

</pre>

<pre>
The string has 22 characters
</pre>

<p>
Output.
</p>

<p>
The <i>MakeLower()</i> and <i>MakeUpper()</i> methods make 
characters lower case and upper case.
</p>

<div class="codehead">cases.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;

int main(int argc, char **argv)
{
  wxString str = wxT("The history of my life");

  wxPuts(str.MakeLower());
  wxPuts(str.MakeUpper());
}
</pre>


<pre>
the history of my life
THE HISTORY OF MY LIFE
</pre>

<p>
Output.
</p>


<h2>Utility functions</h2>


<p>
wxWidgets has several handy utility functions for executing a process, 
getting a home user directory or getting the OS name.
</p>

<p>
In the following example, we execute the ls command. For this, we have 
the wxShell() function. Unix only.
</p>

<div class="codehead">shell.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;
#include &lt;wx/utils.h&gt;

int main(int argc, char **argv)
{

  wxShell(wxT("ls -l"));

}
</pre>


<pre>
total 40
-rwxr-xr-x 1 vronskij vronskij  9028 2007-09-06 22:10 basic
-rw-r--r-- 1 vronskij vronskij    95 2007-09-06 22:09 basic.cpp
-rw-r--r-- 1 vronskij vronskij   430 2007-09-06 00:07 basic.cpp~
-rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console
-rw-r--r-- 1 vronskij vronskij   500 2007-09-05 23:17 console.cpp
-rw-r--r-- 1 vronskij vronskij   485 2007-09-05 23:16 console.cpp~
</pre>

<p>
Output.
</p>


<p>
Next we will we will get the home user directory, os name, user name, 
host name and total free memory.
</p>

<div class="codehead">system.cpp</div>
<pre class="code">
#include &lt;wx/string.h&gt;
#include &lt;wx/utils.h&gt;

int main(int argc, char **argv)
{
  wxPuts(wxGetHomeDir());
  wxPuts(wxGetOsDescription());
  wxPuts(wxGetUserName());
  wxPuts(wxGetFullHostName());

  long mem = wxGetFreeMemory().ToLong();

  wxPrintf(wxT("Memory: %ld\n"), mem);
}
</pre>

<pre>
/home/vronskij
Linux 2.6.20-16-generic i686
jan bodnar
spartan
Memory: 741244928
</pre>

<p>
Output.
</p>


<h2>Time &amp; date</h2>

<p>
In wxWidgets, we have several classes for working with date & time.
</p>

<p>
The example shows current date or time in various formats.
</p>

<div class="codehead">datetime.cpp</div>
<pre class="code">
#include &lt;wx/datetime.h&gt;

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();

  wxString date1 = now.Format();
  wxString date2 = now.Format(wxT("%X"));
  wxString date3 = now.Format(wxT("%x"));

  wxPuts(date1);
  wxPuts(date2);
  wxPuts(date3);
}
</pre>

<pre>
Fri Sep  7 21:28:38 2007
21:28:38
09/07/07
</pre>

<p>
Output.
</p>


<p>
Next we will show current time in different cities.
</p>

<div class="codehead">datetime2.cpp</div>
<pre class="code">
#include &lt;wx/datetime.h&gt;

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();

  wxPrintf(wxT("   Tokyo: %s\n"), now.Format(wxT("%a %T"), 
      wxDateTime::GMT9).c_str());
  wxPrintf(wxT("  Moscow: %s\n"), now.Format(wxT("%a %T"), 
      wxDateTime::MSD).c_str());
  wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"), 
      wxDateTime::CEST).c_str());
  wxPrintf(wxT("  London: %s\n"), now.Format(wxT("%a %T"), 
      wxDateTime::WEST).c_str());
  wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"), 
      wxDateTime::EDT).c_str());
}

</pre>

<pre>
   Tokyo: Sat 05:42:24
  Moscow: Sat 00:42:24
Budapest: Fri 22:42:24
  London: Fri 22:42:24
New York: Fri 16:42:24
</pre>

<p>
Output.
</p>

<p>
The following example shows, how we can add date spans to our date/time. 
We add one month to the current time.
</p>

<div class="codehead">datespan.cpp</div>
<pre class="code">
#include &lt;wx/datetime.h&gt;

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();
  wxString date1 = now.Format(wxT("%B %d %Y"));
  wxPuts(date1);

  wxDateSpan span(0, 1);
  wxDateTime then = now.Add(span);

  wxString date2 = then.Format(wxT("%B %d %Y"));
  wxPuts(date2);

}
</pre>

<pre>
September 07 2007
October 07 2007
</pre>

<p>
Output.
</p>


<h3>Files</h3>

<p>
wxWidgets has several classes to facilitate working with files. This is 
low level access to files, as opposed to working 
with streams. 
</p>

<p>
In the following example, we use the <b>wxFile</b> class to create a 
new file and write data to it. We also test, whether the file is opened. 
Note, that when we create a file, it automatically stays as opened.
</p>

<div class="codehead">createfile.cpp</div>
<pre class="code">
#include &lt;wx/file.h&gt;

int main(int argc, char **argv)
{

  wxString str = wxT("You make me want to be a better man.\n");

  wxFile file;
  file.Create(wxT("quote"), true);

  if (file.IsOpened())
      wxPuts(wxT("the file is opened"));

  file.Write(str);
  file.Close();

  if (!file.IsOpened())
      wxPuts(wxT("the file is not opened"));
}
</pre>


<pre>
$ ls qoute
ls: qoute: No such file or directory

$ ./createfile 
the file is opened
the file is not opened

$ cat quote
You make me want to be a better man.
</pre>

<p>
Output.
</p>

<p>
The <b>wxTextFile</b> is a simple class which allows to work with text 
files on line by line basis.
It is easier to work with this class than with wxFile class. 
</p>

<p>
In the next example, we will print the number of lines in a file, 
first and last lines and finally we will 
read and show the contents of the file.
</p>

<div class="codehead">readfile.cpp</div>
<pre class="code">
#include &lt;wx/textfile.h&gt;

int main(int argc, char **argv)
{

  wxTextFile file(wxT("test.c"));

  file.Open();

  wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
  wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
  wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());

  wxPuts(wxT("-------------------------------------"));

  wxString s;

  for ( s = file.GetFirstLine(); !file.Eof(); 
      s = file.GetNextLine() )
  {
       wxPuts(s);
  }

  file.Close();
}
</pre>


<pre>
Number of lines: 8
First line: #include <glib.h>
Last line: }
-------------------------------------
#include &lt;glib.h&gt;
#include &lt;glib/gstdio.h&gt;

int main() {

g_mkdir("/home/vronskij/test", S_IRWXU);

}
</pre>

<p>
Output.
</p>

<p>
The <b>wxDir</b> class allows us to enumerate files and directories.
</p>

<p>
In the following example, we will print all files and directories available in the 
current working directory.
</p>

<div class="codehead">dir.cpp</div>
<pre class="code">
#include &lt;wx/dir.h&gt;
#include &lt;wx/filefn.h&gt;

int main(int argc, char **argv)
{

  wxDir dir(wxGetCwd());

  wxString file;

  bool cont = dir.GetFirst(&amp;file, wxEmptyString,
      wxDIR_FILES | wxDIR_DIRS);

  while (cont) {
      wxPuts(file);
      cont = dir.GetNext(&amp;file);
  }
}
</pre>

<pre>
$ ./dir
dir
temp
console
basic.cpp
basic
quote
createfile
console.cpp
basic.cpp~
test.c
console.cpp~
</pre>

<p>
Output.
</p>

<p>
In this chapter, we have covered some wxWidgets helper classes.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified September 8, 2007  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

